export interface Position {
  x: number;
  y: number;
}

export interface GameConfig {
  canvasWidth: number;
  canvasHeight: number;
  gridSize: number;
  initialSpeed: number;
}

export const DEFAULT_CONFIG: GameConfig = {
  canvasWidth: 600,
  canvasHeight: 400,
  gridSize: 20,
  initialSpeed: 200,
};

export const getRandomPosition = (config: GameConfig): Position => {
  const maxX = Math.floor(config.canvasWidth / config.gridSize);
  const maxY = Math.floor(config.canvasHeight / config.gridSize);
  
  return {
    x: Math.floor(Math.random() * maxX) * config.gridSize,
    y: Math.floor(Math.random() * maxY) * config.gridSize,
  };
};

export const checkCollision = (pos1: Position, pos2: Position): boolean => {
  return pos1.x === pos2.x && pos1.y === pos2.y;
};

export const checkWallCollision = (pos: Position, config: GameConfig): boolean => {
  return (
    pos.x < 0 ||
    pos.x >= config.canvasWidth ||
    pos.y < 0 ||
    pos.y >= config.canvasHeight
  );
};

export const checkSelfCollision = (head: Position, body: Position[]): boolean => {
  return body.some(segment => checkCollision(head, segment));
};

export const calculateNextPosition = (
  currentPos: Position,
  direction: string,
  gridSize: number
): Position => {
  switch (direction) {
    case 'UP':
      return { ...currentPos, y: currentPos.y - gridSize };
    case 'DOWN':
      return { ...currentPos, y: currentPos.y + gridSize };
    case 'LEFT':
      return { ...currentPos, x: currentPos.x - gridSize };
    case 'RIGHT':
      return { ...currentPos, x: currentPos.x + gridSize };
    default:
      return currentPos;
  }
}; 